package com.example.lynandroid.fragment

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsAnimationCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.lynandroid.R
import com.example.lynandroid.adapter.DirectoryAdapter
import com.example.lynandroid.bean.DirectoryBean
import com.example.lynandroid.databinding.LynBinding
import com.example.lynandroid.getDir
import com.example.lynandroid.log


class InputFragment : Fragment(R.layout.fragment_input) {

    var isKeyBoardShow = false
    var keyBoardHeight = 0
    lateinit var binding: LynBinding

    companion object {
        const val EMOJI_HEIGHT = 608f
        const val MORE_HEIGHT = 1080f
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = LynBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.apply {
            rec.apply {
                layoutManager = LinearLayoutManager(context)
                val list = ArrayList<DirectoryBean>()
                getDir().forEach {
                    list.add(it)
                }
                adapter = DirectoryAdapter(requireActivity(), list)
            }

            board.translationY = EMOJI_HEIGHT

            emoji.setOnClickListener {
                val boardAnimator = ObjectAnimator.ofFloat(board, "translationY", 608f, 0f)
                val relAnimator = ObjectAnimator.ofFloat(rel, "translationY", 0f, -608f)
                val animationSet = AnimatorSet()

                animationSet.duration = 200
                animationSet.playTogether(boardAnimator, relAnimator)
                animationSet.start()
            }

            ViewCompat.setOnApplyWindowInsetsListener(root) { _, insets ->
                val isKeyBoardShow = insets.isVisible(WindowInsetsCompat.Type.ime())
                val keyBoardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
                log("setOnApplyWindowInsetsListener isKeyBoardShow=${isKeyBoardShow}=keyboardhight=${keyBoardHeight}=time=${System.currentTimeMillis()}")
                insets
            }
            ViewCompat.setWindowInsetsAnimationCallback(
                binding.root,
                object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) {
                    override fun onPrepare(animation: WindowInsetsAnimationCompat) {
                        super.onPrepare(animation)
                        val windowInsetsCompat = ViewCompat.getRootWindowInsets(binding.root)
                        val isKeyBoardShow =
                            windowInsetsCompat!!.isVisible(WindowInsetsCompat.Type.ime())
                        val keyBoardHeight =
                            windowInsetsCompat.getInsets(WindowInsetsCompat.Type.ime()).bottom
                        log("onPrepare,isKeyboardSHOW=${isKeyBoardShow}=time=${System.currentTimeMillis()}")
                    }

                    override fun onStart(
                        animation: WindowInsetsAnimationCompat,
                        bounds: WindowInsetsAnimationCompat.BoundsCompat
                    ): WindowInsetsAnimationCompat.BoundsCompat {
                        val windowInsetsCompat = ViewCompat.getRootWindowInsets(binding.root)
                        val isKeyBoardShow =
                            windowInsetsCompat!!.isVisible(WindowInsetsCompat.Type.ime())
                        val keyBoardHeight =
                            windowInsetsCompat.getInsets(WindowInsetsCompat.Type.ime()).bottom
                        log("onStart,isKeyboardSHOW=${isKeyBoardShow}")
                        return bounds
                    }

                    override fun onProgress(
                        insets: WindowInsetsCompat,
                        runningAnimations: MutableList<WindowInsetsAnimationCompat>
                    ): WindowInsetsCompat {
                        val isKeyBoardShow: Boolean =
                            insets.isVisible(WindowInsetsCompat.Type.ime())
                        val keyBoardHeight: Int =
                            insets.getInsets(WindowInsetsCompat.Type.ime()).bottom

                        val imeAnimation = runningAnimations.find {
                            it.typeMask and WindowInsetsCompat.Type.ime() != 0
                        } ?: return insets
                        val i=imeAnimation.interpolatedFraction
                        log("onProgress,isKeyboardSHOW=${isKeyBoardShow},dddd=${imeAnimation.interpolatedFraction}")
                        return insets
                    }
                })
        }
    }
}